package com.siae.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.RoleMapper;
import com.siae.pojo.Role;
import com.siae.pojo.User;
import com.siae.pojo.vo.Result;
import com.siae.service.RoleService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result<List<Role>> getAllRoles(String userRole) {
        try{
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(userRole),"user_role",userRole);
            List<Role> list = roleMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询用户角色列表失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<Role> getRoleById(Integer id) {
        try{
            Role role = roleMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<Role> getRoleByName(String userRole) {
        try{
            if(StringUtils.isBlank(userRole)){
                return Result.fail(BizCode.BLANK_ERROR);
            }
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            wrapper.eq(StringUtils.isNotBlank(userRole),"user_role",userRole);
            Role role = roleMapper.selectOne(wrapper);
            return Result.success(BizCode.SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result addRole(Role role) {
        try{
            role.setAddTime(new Date());
            role.setUpdateTime(new Date());
            role.setIsDel(0);
            roleMapper.insert(role);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("添加用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result updateRole(Role role) {
        try{
            role.setUpdateTime(new Date());
            roleMapper.updateById(role);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("修改用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result deleteRole(Integer id) {
        try{
            roleMapper.deleteById(id);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }
}
